//
//  CrudeOilExchange.m
//  DataCollector
//
//  Created by jiro on 12/06/11.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "CrudeOilExchange.h"

#import "../DbUtility.h"
#import "../DbConstants.h"

@implementation CrudeOilExchange

@synthesize crudeOilCode;
@synthesize price;

+ (id) crudeOilExchangeWithValues: (NSArray*) values
{
	CrudeOilExchange* ret = [ [ CrudeOilExchange alloc ] init ];
	ret.code = [ values objectAtIndex: 0 ];
	ret.crudeOilCode = [ values objectAtIndex: 1 ];
	ret.exchangeDate = [ NSDate dateWithString: [ DbUtility convertIsoStdDateStringToCocoaDateString: [ values objectAtIndex: 2 ] ] ];
	ret.price = [ [ values objectAtIndex: 3 ] floatValue ];
	return ret;
}

+ (NSString*) tableName
{
	return kText_CrudeOilExchangeTable;
}

+ (NSString*) queryCreateTable
{
	NSString* s = [ NSString stringWithFormat: 
		@"create table %@( %@ %@, %@ %@, %@ %@, %@ %@, %@( %@, %@, %@ ) );",
		kText_CrudeOilExchangeTable,
		kText_Code, kText_Text,		//	this means crude oil product country code
		kText_CrudeOilCode, kText_Text,
		kText_ExchangeDate, kText_TimestampWithTimeZone,
		kText_Price, kText_Real,
		kText_PrimaryKey, kText_Code, kText_CrudeOilCode, kText_ExchangeDate
	];
	return s;
}

- (NSString*) sqlInsertStatement
{
	NSString* s = [ NSString stringWithFormat: @"insert into %@ values %@", [ CrudeOilExchange tableName ], [ self queryInsertValue ] ];
	return s;
}

+ (NSString*) querySelectAll
{
	NSString* s = [ NSString stringWithFormat: @"select * from %@;", [ CrudeOilExchange tableName ] ];
	return s;
}

- (NSString*) queryInsert
{
	NSString* s = [ NSString stringWithFormat: @"insert into * values %@;", 
		[ CrudeOilExchange tableName ], [ self queryInsertValue ] ];
	return s;
}

- (NSString*) queryInsertValue
{
	NSString* s = [ NSString stringWithFormat: @"( %@, %@, %@, %f )", code, crudeOilCode, exchangeDate, price ];
	return s;
}

@end
